import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress' // 引入一份进度条插件
import 'nprogress/nprogress.css' // 引入进度条样式

const whiteList = ['/login']
// 前置守卫
router.beforeEach(async (to, from, next) => {
  if (store.getters.token) {
    //
    if (to.path === '/login') {
      next('/')
    } else {
      // 看有没有用户资料 没有就调用用户资料
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
// 后置守卫
router.afterEach(function () {
  NProgress.done() // 关闭进度条
})
